package work.jame.blog.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import work.jame.blog.framework.VerificationGroup;
import work.jame.blog.framework.result.Result;
import work.jame.blog.pojo.UserArticleCollect;
import work.jame.blog.service.UserArticleCollectService;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jame
 * @since 2023-05-18
 */
@Api(tags = "用户收藏")
@RestController
@RequestMapping("/userArticleCollect")
public class UserArticleCollectController {

    @Autowired
    private UserArticleCollectService articleCollectService;


    @ApiOperation("更新收藏")
    @PostMapping("/update")
    public Result<?> update(@Validated(VerificationGroup.Update.class) @RequestBody List<UserArticleCollect> userArticleCollects) {
        articleCollectService.update(userArticleCollects);
        return Result.succeed();
    }


    @ApiOperation("添加收藏")
    @PostMapping("/save")
    public Result<?> save(@Validated(VerificationGroup.Save.class) @RequestBody UserArticleCollect userArticleCollect) {
        if (articleCollectService.save(userArticleCollect)) {
            return Result.succeed();
        }
        return Result.fail();
    }

    @ApiOperation("移除收藏")
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam("groupId") Long groupId,
                            @RequestParam("articleId") Long articleId) {
        if (articleCollectService.delete(groupId, articleId)) {
            return Result.succeed();
        }
        return Result.fail();
    }


    @ApiOperation("移除该文章的所有收藏")
    @GetMapping("/removeAllCollect")
    public Result<?> removeAllCollect(@RequestParam("articleId")Long articleId){
        if(articleCollectService.removeAllCollectByArticleId(articleId)){
            return Result.succeed();
        }
        return Result.fail();
    }

}
